#include "Chat.h"
#include "cinder/app/AppBasic.h"
#include "cinder/Utilities.h"
#include "Global.h"
#include "cinder/Surface.h"
#include "cinder/gl/Texture.h"
#include "cinder/gl/gl.h"

using namespace cinder;

Chat::Chat()
    : chatCounter(0),
      opacity(0)
{
    //textLayout.setBorder(Curse::screenWidth*0.25f,Curse::screenHeight*0.25f);
     mTextureFont = gl::TextureFont::create( Font( app::loadResource("/resources/OCRAEXT.ttf"), 36 ) );
     //mTextureFont = gl::TextureFont::create( Font( "Trebuchet MS", 36 ) );
}

void Chat::draw()
{
    if(chatCounter > 0) {


        gl::enableAlphaBlending(true);
        if(chatCounter > 150) {
            opacity = opacity + 1;
            //gl::color(ColorA(0,0,0,(opacity/30)*0.5));
            //gl::drawSolidRect(Rectf(0,0,Curse::screenWidth*0.33f,Curse::screenHeight));
        } else if(chatCounter < 30) {
            opacity = opacity - 1;
            //gl::color(ColorA(0,0,0,(opacity/30)*0.5));
            //gl::drawSolidRect(Rectf(0,0,Curse::screenWidth*0.33f,Curse::screenHeight));
        } else {
            opacity = 30;
            //gl::color(ColorA(0,0,0,(opacity/30)*0.5));
            //gl::drawSolidRect(Rectf(0,0,Curse::screenWidth*0.33f,Curse::screenHeight));
        }


        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ONE_MINUS_SRC_COLOR);
        float cVal = ((float)opacity/30.0f)*1.0f;
        float totalLength = Curse::screenHeight-20;
        for(int i=chats.size()-1;i>=0;i-=1) {
            //gl::drawString(chats.at(i).toStdString(),Vec2f(10,Curse::screenHeight-20 - ((chats.size()-i)*60)),ColorA(1,1,1,(opacity/30)),font);
            gl::color( ColorA( cVal,cVal,cVal,cVal ) );
            //DrawOptions options;
            //mTextureFont->drawStringWrapped( chats.at(i), Rectf(10,Curse::screenHeight-20 - ((chats.size()-i)*100),Curse::screenWidth*0.3f,Curse::screenHeight),Vec2f::zero() );
            float length = ((string)chats.at(i)).length();
            if(length <= 50) {
                totalLength = totalLength - 50;
            } else if( length > 50 && length < 100) {
                totalLength = totalLength - 100;
            } else if (length > 100 && length < 150) {
                totalLength = totalLength - 150;
            } else if (length > 150 && length < 200) {
                totalLength = totalLength - 200;
            } else if (length > 250 && length < 300) {
                totalLength = totalLength - 250;
            }
            mTextureFont->drawStringWrapped( chats.at(i), Rectf(10,totalLength,Curse::screenWidth,Curse::screenHeight),Vec2f::zero() );

        }
        glDisable(GL_BLEND);
        chatCounter = chatCounter -1;
    } else if(chatCounter < 0) {
        chatCounter = 0;
    }
}

void Chat::receiveChat(string chatStr)
{
    //this->chatStr.
    //this->chatStr.append(chatStr);
    //this->chatStr = chatStr;
    if(chats.size() > 10) {
        chats.erase(chats.begin());
    }
    chats.push_back(chatStr);
    //chats.append(chatStr);

    //gl::TextureFont::create()
    //textLayout.addLine(chatStr.toStdString());
    //gl::Texture(textLayout.render());
    //Surface chatSurface = textLayout.render(true,false);
    //gl::Texture tex(textLayout.render());
    chatCounter = 180;
}
